package control;

import Util.PageUtil;
import bean.CClass;
import bean.TArea;
import com.google.gson.Gson;
import serves.Impl.areaSevImpl;
import serves.Impl.classSevImpl;
import serves.areaSev;
import serves.classSev;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "classServlet",urlPatterns = "/classM")
public class classServlet extends HttpServlet {
   classSev cs =new classSevImpl();
    areaSev as = new areaSevImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        if(method==null||method.equalsIgnoreCase("")) {
            classShow(request, response);
        }else if(method.equalsIgnoreCase("add")){
            classAdd(request,response);
        }else if(method.equalsIgnoreCase("alt")){
            classAlt(request,response);
        }
    }
    
    private void classAlt(HttpServletRequest request, HttpServletResponse response) {
        Long id =Long.parseLong(request.getParameter("cId"));
        String cRse = request.getParameter("mainName");
        String cCel = request.getParameter("tel");
        CClass cc  = new CClass();
        cc.setCRes(cRse);
        cc.setCCel(cCel);
        cc.setCId(id);
        boolean flag = cs.alter(cc);

    }

    private void classAdd(HttpServletRequest request, HttpServletResponse response) {
        String className = request.getParameter("className");
        String cRse = request.getParameter("mainName");
        String cCel = request.getParameter("tel");
        String cDis = request.getParameter("mainArea");

        CClass cc  = new CClass();
        cc.setCName(className);
        cc.setCRes(cRse);
        cc.setCCel(cCel);
        cc.setCDis(cDis);
        cs.add(cc);

        CClass newC = cs.find(cc);

        cs.altArea(newC);

    }

    private void classShow(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        int pn =1;
        String pageNo = request.getParameter("pageNo");
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        System.out.println("pageSize = " + pageSize);
        if(pageNo==null){
            pn =1;
        }else{
            pn=Integer.parseInt(pageNo);
        }
        String className = request.getParameter("className");
        PageUtil<CClass> areaList = cs.ListByPage(pn,pageSize,className);
        Gson g = new Gson();
        String json =g.toJson(areaList);
        System.out.println(json);
        pw.write(json);
    }
}
